package com.erp.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.common.R;
import com.erp.entity.Goods;
import com.erp.entity.Order;
import com.erp.entity.Purchase;
import com.erp.service.OrderService;
import com.erp.service.PurchaseService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/purchase")
@RequiresRoles(value = {"SupperAdministrator","LogisticsAdministrator"} ,logical = Logical.OR )
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    @PostMapping
    public R<String> save(@RequestBody Purchase purchase) {
        boolean save = purchaseService.save(purchase);
        if (!save) {
            return R.error("error");
        }
        return R.success("采购新增成功");
    }

    @DeleteMapping
    public R<String> delete(Long id) {
        boolean b = purchaseService.removeById(id);
        if (!b) {
            return R.error("error");
        }
        return R.success("采购删除成功");
    }

    @GetMapping("/{id}")
    public R<Purchase> find(@PathVariable Long id) {
        Purchase byId = purchaseService.getById(id);
        return R.success(byId);
    }



    @GetMapping("/page")
    public R<Page<Purchase>> findpage(int page, int pageSize,String name) {
        LambdaQueryWrapper<Purchase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(Purchase::getCreateDateTime);
        if (name!=null){
            lambdaQueryWrapper.like(Purchase::getGoods,name);
        }
        Page<Purchase> orderPage = new Page<>(page,pageSize);
        purchaseService.page(orderPage,lambdaQueryWrapper);
        return R.success(orderPage);
    }



    @PutMapping
    public R<String> update(@RequestBody Purchase purchase){

        boolean b = purchaseService.updateById(purchase);
        if (!b) {
            return R.error("error");
        }
        return R.success("采购更改成功");
    }

}

